package benny_driving_ser.util;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import benny_driving_ser.ObjectBean.PayCodeBean;

public class CalcPayCodeCostUtil {
	
	public static int CalcPayCodeCost(String payCode,Connection conn){
		int cost = 0;
		PayCodeBean payCodeBean = null;
		//Connection conn = DBUtil.getInstance().openConnection();
		String sql_our = "select * from t_paycode where paycode="+"'"+payCode+"'";
		String sql_outside = "select * from t_paycode where codeSource="+"'"+payCode+"'";
		try {
			PreparedStatement pst_our = conn.prepareStatement(sql_our);
			ResultSet rs_our = pst_our.executeQuery();
			PreparedStatement pst_outside= conn.prepareStatement(sql_outside);
			ResultSet rs_outside = pst_outside.executeQuery();
			if(rs_our.next()){
				payCodeBean = new PayCodeBean(
					rs_our.getString("id"), 
					rs_our.getString("codeSource"), 
					rs_our.getString("payCode"),
					rs_our.getString("fullPayCode"),
					rs_our.getString("payType"),
					rs_our.getString("payCost"),
					rs_our.getString("wait"), 
					rs_our.getString("addTime"), 
					rs_our.getString("used"),
					rs_our.getString("times"), 
					rs_our.getString("remainCost"), 
					rs_our.getString("dayOrNight"),
					rs_our.getString("isActivate"));
			}else if(rs_outside.next()){
				payCodeBean = new PayCodeBean(
					rs_outside.getString("id"), 
					rs_outside.getString("codeSource"), 
					rs_outside.getString("payCode"),
					rs_outside.getString("fullPayCode"),
					rs_outside.getString("payType"),
					rs_outside.getString("payCost"),
					rs_outside.getString("wait"), 
					rs_outside.getString("addTime"), 
					rs_outside.getString("used"),
					rs_outside.getString("times"), 
					rs_outside.getString("remainCost"), 
					rs_outside.getString("dayOrNight"),
					rs_outside.getString("isActivate"));
			}
			
			if(payCodeBean!=null){
				
				System.out.println("进入计算逻辑.");
				switch(Integer.parseInt(payCodeBean.getPayType())){
				case 1://里程
					try {
						cost = CalcDrivingCostUtil.getTaximeterPrivilege(Integer.parseInt(payCodeBean.getDayOrNight()), Integer.parseInt(payCodeBean.getPayCost()));
					} catch (NumberFormatException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					break;
				case 2://金额
					cost = Integer.parseInt(payCodeBean.getPayCost());
					break;
				case 3://等候时间
					cost = Integer.parseInt(payCodeBean.getPayCost())-5;
					break;
				}
				rs_our.close();
				pst_our.close();
				rs_outside.close();
				pst_outside.close();
//				conn.close();
				System.out.println("结束计算逻辑...");
				return cost;
			}else{
				return cost;
			}
			
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
		System.out.println("支付码不可用..");
		return cost;
	}

//	public static void main(String[] args) {
//		System.out.println(CalcPayCodeCost("b105c56df",DBUtil.getInstance().openConnection()));
//	}
}
